package com.yanqu.road.server.cmd.palace;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.palace.CrossPalaceActivity;
import com.yanqu.road.server.manager.palace.CrossPalaceActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_PALACE_AIDE_JOIN_APPLY, desc = "幕宾加入请求")
public class CrossPalaceAideJoinApplyCmd extends CrossCommand {
    private int activityId;
    private long applyUserId;
    private UserBaseInfo applyUserInfo;
    private long destUserId;
    private long destServerId;
    private boolean isSameUnion;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceAideJoinCrossReqMsg reqMsg = EstateWarProto.PalaceAideJoinCrossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        applyUserId = reqMsg.getApplyUserId();
        applyUserInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getApplyUserInfo());
        destUserId = reqMsg.getUserId();
        destServerId = reqMsg.getServerId();
        isSameUnion = reqMsg.getIsSameUnion();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossPalaceActivity crossPalaceActivity = CrossPalaceActivityMgr.getCrossPalaceActivity(activityId);
        if(null == crossPalaceActivity){
            ret = GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND;
            return;
        }
        ret = crossPalaceActivity.applyAide(applyUserId, applyUserInfo, destUserId, destServerId, isSameUnion);
    }

    @Override
    public void sendData() {
        EstateWarProto.PalaceAideJoinRespMsg.Builder respMsg = EstateWarProto.PalaceAideJoinRespMsg.newBuilder();
        respMsg.setRet(ret);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_PALACE_AIDE_JOIN, respMsg);
        MessageHelper.sendPacket(serverId, applyUserId, pbMsg);
    }
}
